package com.lemon.boot.common.handler;

import com.lemon.boot.common.utils.RequestUtil;
import lombok.Getter;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @author 李猛
 * @datetime 2024/3/6 18:16
 * @description 解决 request重复读问题
 */
@Getter
public class HttpServletRequestHandler extends HttpServletRequestWrapper {
    private final byte[] body;

    public HttpServletRequestHandler(HttpServletRequest request) {
        super(request);
        this.body = RequestUtil.getByteBody(request);
    }

    @Override
    public ServletInputStream getInputStream() {
        return new ServletInputStreamHandler(body);
    }

    @Override
    public BufferedReader getReader() {
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }
}
